package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument.BranchElement;

import dao.UserDao;
import model.PersonModel;

/** 
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		String content1="";
	    String content2="";
	    String path="";
		boolean flag=false;
	
		
	    request.setCharacterEncoding("UTF-8");
	  
	   String code = request.getParameter("code");
	   Object authCode = String.valueOf(request.getSession().getAttribute("authCode"));
	   
	  
	   String username = request.getParameter("username");
	   String password = request.getParameter("password");
  
	   if(authCode.equals(code)) {
		
	       
		   UserDao userDao = new UserDao();
		   
		   PersonModel person =  new PersonModel();
		   
		   person.setUsername(username);
		   
		   person =  userDao.selectSingle(person);
		   
		   if(person.getUsername()==null) {  // 转发账号不存在界面
			   flag=true;
			  content1 ="对不起，该账号不存在";
			  content2="返回登录";
			  path="/Test2022/web/js/login.jsp";
			   
			   
		   }else { //登录成功
			   String password2 = person.getPassword();
			   if(password.equals(password2)) {
				   
//				    int id = person.getId();
//					String sex = person.getSex();
//					String hobby = person.getHobby();
//					String address = person.getAddress();
//				   request.getSession().setAttribute("id", id);
//					request.getSession().setAttribute("username", username);
//					request.getSession().setAttribute("password", password);
//					request.getSession().setAttribute("sex", sex);
//					request.getSession().setAttribute("hobby", hobby);
//					request.getSession().setAttribute("address", address);
				    
				   ArrayList<PersonModel> list = userDao.selectAll();
				   
				
				   request.getSession().setAttribute("list", list);
				   

				   response.sendRedirect("/Test2022/web/js/showAll.jsp");
				
				  
				   
				  
			   }else {//转发密码错误界面
				   flag=true;
				   content1="密码错误，请重新输入";
				   content2="返回登录";
				   path="/Test2022/web/js/login.jsp";
				
				
				
				
			}
		}
		   
		 
		
		   
//		   response.sendRedirect("/Test2020/index.jsp");
	   }else {
		flag=true;
		   content1="验证码输入错误，请重新输入";
		   content2="返回登录";
		   path="/Test2022/web/js/login.jsp";
		   
		 
		 
	}
	   if (flag) {

		    request.getSession().setAttribute("content1", content1);
			request.getSession().setAttribute("content2", content2);
			request.getSession().setAttribute("path", path);
			
			response.sendRedirect("/Test2022/web/js/PasswordWrong.jsp");
	}
		
	}

	
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
